import React, {} from 'react'
import { StyleSheet } from 'react-native'
import MessageList from 'src/components/message/MessageList'
import { getUserAvatorLocalB } from 'src/utils/ConfigUtil'
import CustomButton from 'src/components/button/CustomButton'
import MessageItem from 'src/components/message/MessageItem'
import NavigationUtil from 'src/utils/NavigationUtil'
import { postRecommendTitle } from 'src/utils/RequestAPI'

export default class MessageComponent extends MessageList {
  constructor (props) {
    super(props)
    this.refresh = true
    if (this.componentWillFocus) {
      this.componentWillFocus = this.componentWillFocus.bind(this)
      this.willFocusSubscription = props.navigation.addListener(
        'willFocus',
        this.componentWillFocus
      )
    }
    this.state = {
      ...this.state,
      recommendTitle: {},
      showEmpty: false
    }
  }

  getErrorSource (item) {
    return getUserAvatorLocalB(item.gender)
  }

  listHeaderComponent = () => {
    const { recommendTitle } = this.state
    const { userId } = this.props
    if (Object.keys(recommendTitle).length > 0 && userId) {
      const item = {
        nick: recommendTitle.companyName,
        lastMsg: recommendTitle.postName,
        msgTime: recommendTitle.createTime,
        avatarUrl: ''
      }
      return (
        <CustomButton style={styles.headerView} onPress={this._onHeaderPress}>
          <MessageItem errorSource={require('src/imgC/message/notify_icon_1.png')} item={item} needFormatTime={true}/>
        </CustomButton>
      )
    } else {
      return null
    }
  }

  _onHeaderPress = () => {
    NavigationUtil.navigate('RecommendList')
  }

  componentDidMount () {

  }

  componentWillFocus () {
    this._postRecommendTitle()
  }

  /**
   * 刷新头部通知
   * @private
   */
  _postRecommendTitle = () => {
    const { userId, landmark: { cityName, currentLocation: { lat: latitude, lng: longitude } = {} } } = this.props
    if (!userId) {
      this.setState({
        showEmpty: true
      })
      return
    }
    postRecommendTitle({
      params: {
        city: cityName,
        latitude,
        longitude,
        userId
      }
    }).then((data) => {
      console.info(data)
      // todo 判断对象是否有值
      this.setState({
        recommendTitle: data.data,
        showEmpty: Object.keys(data.data).length <= 0
      })
    })
  }

  onRefresh () {
    this._postRecommendTitle()
  }
}

const styles = StyleSheet.create({
  headerView: {}
})
